package com.dkm.dkmproject.action;


import com.dkm.dkmproject.domain.EvaluationTb;
import com.dkm.dkmproject.domain.UserTb;
import com.dkm.dkmproject.service.IEvaluationTbService;
import com.dkm.dkmproject.service.IUserTbService;
import com.dkm.dkmproject.utils.Message;
import com.dkm.dkmproject.utils.Tools;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 评价表 前端控制器
 * </p>
 *
 * @author 周佳佳
 * @since 2020-04-26
 */
@RestController
@RequestMapping("/dkm.dkmproject/evaluationTb")
@Api(description = "评价接口文档")
public class EvaluationTbController {

    @Resource
    private IEvaluationTbService iEvaluationTbService;

    @Autowired
    IUserTbService userTbService;
    @Resource
    Tools tools;
    /**
     * 根据订单id查询订单评价
     *
     * @return
     */
    @ApiOperation(value = " 根据订单id查询订单评价", notes = "如果查询成功返回集合，失败返回为null")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "buId", value = "订单主键"),
    })
    @ApiResponses({
            @ApiResponse(code = 401, message = "没有权限"),
            @ApiResponse(code = 403, message = "服务器拒绝请求"),
            @ApiResponse(code = 404, message = "请求路径没有或页面跳转路径不对"),
            @ApiResponse(code = 500, message = "后台报错"),
            @ApiResponse(code = 200, message = "返回成功")
    })
    @GetMapping("/QueryEvaluation")
    public List<EvaluationTb> QueryEvaluation(@RequestParam(value = "buId") Integer buId){
        List<EvaluationTb> list=new ArrayList<>();
        if(buId!=null){
            List<EvaluationTb> evaluationTbs = iEvaluationTbService.QueryEvaluation(buId);
            for (EvaluationTb evaluationTb : evaluationTbs) {
                evaluationTb.setEvDateOne(tools.getTimeStr(evaluationTb.getEvDate()));
                list.add(evaluationTb);
            }
            return list;
        }
        return null;

    }


    /**
     * 增加评价
     *
     * @return
     */
    @ApiOperation(value = " 根据订单id查询订单评价", notes = "如果查询成功返回集合，失败返回为null")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "orId", value = "订单主键"),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "evContent", value = "评论内容"),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "evScore", value = "服务态度评星"),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "ev_scoreone", value = "服务速度评星"),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "evScoretwo", value = "服务准备评星"),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "evPerson", value = "评论人"),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "userId", value = "用户主键"),
    })
    @ApiResponses({
            @ApiResponse(code = 401, message = "没有权限"),
            @ApiResponse(code = 403, message = "服务器拒绝请求"),
            @ApiResponse(code = 404, message = "请求路径没有或页面跳转路径不对"),
            @ApiResponse(code = 500, message = "后台报错"),
            @ApiResponse(code = 200, message = "返回成功")
    })
    @GetMapping("/AddEvaluation")
    public Message AddEvaluation(EvaluationTb evaluationTb){
        List<UserTb> list=userTbService.selectUserId(evaluationTb.getUserId());
        for (UserTb userTb : list) {
            evaluationTb.setEvPerson(userTb.getWechatName());
            evaluationTb.setEvImg(userTb.getWecahtWechatimg());
        }
        Message message=new Message();
            int insert = iEvaluationTbService.insertSelective(evaluationTb);
            if(insert>0){
                message.setMsg("增加评价成功");
                message.setNum(1);
            }else{
                message.setMsg("增加评价失败");
                message.setNum(0);
            }
            return message;
    }
    @RequestMapping("selectAllTwo")
    @ResponseBody
    public Map<String,Object> selectAllTwo(@RequestParam("page") Integer pageNum , @RequestParam("limit") Integer pageSize){
        Map<String,Object> map=new HashMap<>();
        map.put("data",iEvaluationTbService.selectAll(pageNum,pageSize));
        map.put("count",iEvaluationTbService.selectCountTwo());

        map.put("msg","");

        map.put("code",0);
        return map;
    }

    @ApiOperation(value = " 根据申请服务的id 是否评价过", notes = "如果评价过返回1 没有则为0")
    @RequestMapping("selectCount")
    @ResponseBody
    public Message selectCount(Integer buId){
        Message message=new Message();
        int rows=iEvaluationTbService.selectCountThree(buId);
        if(rows<=0){
            message.setNum(1);
            message.setMsg("没有评论过");
        }else{
            message.setNum(0);
            message.setMsg("评论过");
        }
        return message;
    }
}
